package com.aps.server.service;

import com.alibaba.fastjson2.JSON;
import com.aps.algorithm.entity.BasicEquipSchedulingResult;
import com.aps.algorithm.mapper.BasicEquipSchedulingResultMapper;
import com.aps.common.page.PageQuery;
import com.aps.common.redis.RedisUtils;
import com.aps.common.service.TestGenericService;
import com.aps.server.context.RedisKeyConstant;
import com.aps.server.context.ValidateContext;
import com.aps.server.dto.EquipSchedulingResultDto;
import com.aps.server.dto.PlanComparisonDto;
import com.aps.server.dto.QueryParamEquipScheduling;
import com.aps.server.dto.SchedulingPlanGenerateDto;
import com.aps.server.mapper.BasicEquipSchedulingResultBizMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author nbZhuozh
 * @Date 2025-10-23  14:04
 **/
@Service
@Slf4j
public class PlanComparisonService extends TestGenericService {

    @Autowired
    BasicEquipSchedulingResultBizMapper bizMapper;

    @Autowired
    BasicEquipSchedulingResultMapper basicEquipSchedulingResultMapper;


    /**
     * 获取已生成的计划配置
     */
    public List<SchedulingPlanGenerateDto> queryConstraintList(String constraintIds){
        List<String> constraintIdList = Arrays.asList(constraintIds.split(","));
        String defaultKey = RedisKeyConstant.getDefaultKey(RedisKeyConstant.ALGOR_REDIS_CONSTRAINT_KEY, getCurrentTenantId());
        Map<String, Object> cacheMap = RedisUtils.getCacheMap(defaultKey);
        List<SchedulingPlanGenerateDto> result = new ArrayList<>();
        for (String s : constraintIdList) {
            if(cacheMap.containsKey(s)){
                SchedulingPlanGenerateDto schedulingPlanGenerateDto = JSON.parseObject(String.valueOf(cacheMap.get(s)), SchedulingPlanGenerateDto.class);
                result.add(schedulingPlanGenerateDto);
            }
        }
        return result;
    }

    /**
     * 获取计划详情
     * @param pg
     * @return
     */
    public IPage<EquipSchedulingResultDto> queryPlanListByConfigId(PageQuery<QueryParamEquipScheduling> pg){
        QueryParamEquipScheduling condition = pg.getCondition();
        condition.setTenantId(getCurrentTenantId());
        return bizMapper.queryPlanInfo(pg.build(), condition);
    }


    /**
     * 根据配置查询对应计划明细
     * @param constrainId
     */
    private BasicEquipSchedulingResult queryPlanByConstraintIds(String constrainId) {
        if(StringUtils.isEmpty(constrainId)){
            return null;
        }
        String defaultKey = RedisKeyConstant.getDefaultKey(RedisKeyConstant.ALGOR_REDIS_PLAN_DATA_KEY, getCurrentTenantId());
        Map<String, Object> cacheMap = RedisUtils.getCacheMap(defaultKey);
        if(cacheMap.containsKey(constrainId)){
            String plan = (String)cacheMap.get(constrainId);
            return JSON.parseObject(plan, BasicEquipSchedulingResult.class);
        }
        return null;
    }

    /**
     * 根据配置查询计划配置
     */


}
